package saxion.security.android;

import java.io.IOException;

import javax.crypto.SealedObject;

import nl.saxion.security.encryption.TransformerAES;
import nl.saxion.security.synchronize.ObjectSerializer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class InstallActivity extends Activity {
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;
	private EditText password1Text;
	private EditText password2Text;
	private TransformerAES transformer;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.install);
	    
	    //find id's from xml
	    Button installButton = (Button)findViewById(R.id.install_button);
	    password1Text = (EditText)findViewById(R.id.savePassword1);
	    password2Text = (EditText)findViewById(R.id.savePassword2);
	    
	    password1Text.requestFocus();//set focus to the first edittext
	    
	    //create the transformer class
	    transformer = new TransformerAES();
	   
	    installButton.setOnClickListener(buttonSendOnClickListener);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
                

	}
	
	/**
	 * Onclick listener for the "Start" button
	 */
	Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener(){

		public void onClick(View arg0) {

			String pw1 = password1Text.getText().toString();
			String pw2 = password2Text.getText().toString();
			
			//options used by the toast
			Context context = getApplicationContext();
			int duration = Toast.LENGTH_SHORT;
			
			if(pw1.equals(pw2)){
				
				if(pw1.length() > 0 && pw2.length() > 0){
					
					//generate a randomw key
					byte[] key = null;
				    try {
						key = transformer.generateRandomKey();
					} catch (Exception e) {
						e.printStackTrace();
					}
				    
				    byte[] hashFromPwd;
				    SealedObject sealedKey = null;
				    
				    //create an hash from the password en encrypt the genereted key
					try {
						hashFromPwd = transformer.generateKey(pw1);
						sealedKey = transformer.encryptObject(hashFromPwd, key);
					} catch (Exception e) {
						e.printStackTrace();
					}
				    
			        editor = prefs.edit(); //set the preferences in edit mode
				    //encrypt the generated key with your password		
				    try {
						editor.putString("sealedKey", ObjectSerializer.serialize(sealedKey));
					} catch (IOException e) {
						e.printStackTrace();
					}
			        editor.putBoolean("install",false);
			        editor.commit(); // Very important, close the preferences editor
					
					//start the main activity and stop this one				
			        Intent mainIntent = new Intent(InstallActivity.this, SecurityAndroidActivity.class);
			        startActivity(mainIntent);
			        InstallActivity.this.finish();
				}else{
					//show a toast when both passwords fields are empty
					Toast toast = Toast.makeText(context, "Er is niets ingevuld", duration);
					toast.show();
				}
			}else{
				//show a toast when both passwords not the same
				Toast toast = Toast.makeText(context, "Wachtwoorden komen niet overeen", duration);
				toast.show();
			}

		}
	};
}
